package com.sunni.mapper;

import com.sunni.entity.Employee;
import com.sunni.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    @Insert("insert into `user` values(#{id},#{name})")
    void save(User user);

    @Delete("delete from `user` where id=#{id}")
    void delete(Integer id);

    @Update("update `user` set name=#{name} where id=#{id}")
    void update(User user);

    @Select("select * from `user` where id=#{id}")
        /*一对一查询的order的uid，作为#{id}的值*/
        /*这条sql的查询结果集可以封装成一个user, 正好是Order类的user属性需要的*/
    User selectById(Integer id);

    @Select("select * from `user`")
    List<User> selectAll();

    /*day16-06 : 一对多
    思路 ：
    1. SELECT * FROM `user`
    2. (把user表的id 当做order表的uid条件) 去查询所有order表的数据 结果集封装成orderList属性
    */
    @Select("SELECT * FROM `user`")
    @Results({
            /*User 基本属性*/
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "name",column = "name"),
            @Result(property = "address",column = "address"),
            @Result(property = "birthday",column = "birthday"),
            /*orderList属性 调用order的方法 查出(这个uid)所有订单 结果集封装成orderList*/
            @Result(
                    javaType = List.class,
                    property = "orderList",
                    column = "id",/* 把 user的id 当成参数   ，传给order的selectByUid方法（这个方法可以封装orderList订单集合）*/
                    many = @Many(select = "com.sunni.mapper.OrderMapper.selectByUid")
            )
    })
    List<User> selectUserAllAndOrderList();
}
